package com.itheima.web;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.Permission;
import com.itheima.domain.UserInfo;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping(path = "/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @RequestMapping("/findAll")
    public String findAll(Model model, @RequestParam(name = "page", required = true, defaultValue = "1") int page
            , @RequestParam(name = "pageSize", required = true, defaultValue = "4") int pageSize) {
        List<Permission> permissionList = permissionService.findAll(page, pageSize);
        PageInfo pageInfo = new PageInfo(permissionList);
        model.addAttribute("permissionList", pageInfo);
        return "permission-list";
    }
    @RequestMapping("/save")
    public String save( Permission permission) {
        System.out.println(permission.getPermissionName());
        System.out.println(permission.getUrl());
        int num=  permissionService.save(permission);
        if(num!=0){
            return "redirect:/permission/findAll";
        }
        return "permission-add";
    }

    @RequestMapping("/findById")
    public String findById(Model model, String id) {
        Permission permission = permissionService.findById(id);
        model.addAttribute("permission", permission);
        return "permission-show";
    }
}
